package cn.dzhang.rcp.hyperbola.views;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.math.RandomUtils;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.viewers.CheckStateChangedEvent;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.ICheckStateListener;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.part.ViewPart;

import cn.dzhang.rcp.hyperbola.adapters.TableViewerContentProvider;
import cn.dzhang.rcp.hyperbola.adapters.TableViewerLabelProvider;
import cn.dzhang.rcp.hyperbola.common.UIConstants;
import cn.dzhang.rcp.hyperbola.common.UIUtils;
import cn.dzhang.rcp.hyperbola.model.Alarm;
import cn.dzhang.rcp.hyperbola.model.AlarmLevel;
import cn.dzhang.rcp.hyperbola.model.ListHolder;

public class AlarmView extends ViewPart {

	public static final String ID = "cn.dzhang.rcp.hyperbola.views.AlarmView"; //$NON-NLS-1$
	private Table tblAlarm;
	private CheckboxTableViewer chktblvwAlarm;
	private final List<Alarm> alarms = new ArrayList<Alarm>();
	private Action actionPack;

	public AlarmView() {
	}

	/**
	 * Create contents of the view part.
	 * 
	 * @param parent
	 */
	@Override
	public void createPartControl(Composite parent) {
		parent.setLayout(new GridLayout(1, false));

		chktblvwAlarm = CheckboxTableViewer.newCheckList(parent, SWT.BORDER
				| SWT.FULL_SELECTION);
		tblAlarm = chktblvwAlarm.getTable();
		tblAlarm.setHeaderVisible(true);
		tblAlarm.setLinesVisible(true);
		tblAlarm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		TableColumn tblclmnNo = new TableColumn(tblAlarm, SWT.NONE);
		tblclmnNo.setWidth(100);
		tblclmnNo.setResizable(false);
		tblclmnNo.setText("No");

		TableColumn tblclmnName = new TableColumn(tblAlarm, SWT.NONE);
		tblclmnName.setWidth(100);
		tblclmnName.setText("Name");

		TableColumn tblclmnLevel = new TableColumn(tblAlarm, SWT.NONE);
		tblclmnLevel.setWidth(100);
		tblclmnLevel.setText("Level");

		TableColumn tblclmnObject = new TableColumn(tblAlarm, SWT.NONE);
		tblclmnObject.setWidth(100);
		tblclmnObject.setText("Object");

		TableColumn tblclmnTime = new TableColumn(tblAlarm, SWT.NONE);
		tblclmnTime.setWidth(100);
		tblclmnTime.setText("Time");

		TableColumn tblclmnDescription = new TableColumn(tblAlarm, SWT.NONE);
		tblclmnDescription.setWidth(100);
		tblclmnDescription.setText("Description");

		Composite cmpstButtons = new Composite(parent, SWT.NONE);
		cmpstButtons.setLayout(new GridLayout(1, false));
		cmpstButtons.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));

		Button btnRefresh = new Button(cmpstButtons, SWT.NONE);
		btnRefresh.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				List<Alarm> alarms = generateAlarms();

				AlarmView.this.alarms.clear();
				AlarmView.this.alarms.addAll(alarms);
				chktblvwAlarm.refresh();
			}
		});
		GridData gd_btnRefresh = new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 1, 1);
		gd_btnRefresh.widthHint = 100;
		btnRefresh.setLayoutData(gd_btnRefresh);
		btnRefresh.setText("Refresh");

		chktblvwAlarm.setLabelProvider(new TableViewerLabelProvider());
		chktblvwAlarm.setContentProvider(new TableViewerContentProvider());
		chktblvwAlarm.setInput(new ListHolder<Alarm>(alarms));

		createActions();
		initializeToolBar();
		initializeMenu();
		initializeListeners();
	}

	private void initializeListeners() {
		chktblvwAlarm.addCheckStateListener(new ICheckStateListener() {
			@Override
			public void checkStateChanged(CheckStateChangedEvent event) {

			}
		});
	}

	protected List<Alarm> generateAlarms() {
		List<Alarm> alarms = new ArrayList<Alarm>();
		for (int i = 0; i < 100; i++) {
			Alarm alarm = new Alarm();
			alarm.setId(Long.valueOf(i + 1));
			alarm.setDatetime(new Date());
			alarm.setName("告警名称" + i);
			alarm.setDescription("告警描述" + i);
			alarm.setLevel(AlarmLevel.values()[RandomUtils.nextInt(AlarmLevel
					.values().length)]);
			alarm.setObject("告警对象#" + i);

			alarms.add(alarm);
		}

		return alarms;
	}

	/**
	 * Create the actions.
	 */
	private void createActions() {
		// Create the actions
		actionPack = new Action("Pack Table Column",
				UIConstants.ImageDescriptors.SHRINK_16) {
			@Override
			public void run() {
				UIUtils.packTableColumn(tblAlarm);
			}
		};
	}

	/**
	 * Initialize the toolbar.
	 */
	private void initializeToolBar() {
		IToolBarManager toolbarManager = getViewSite().getActionBars()
				.getToolBarManager();
		toolbarManager.add(actionPack);
	}

	/**
	 * Initialize the menu.
	 */
	private void initializeMenu() {
		IMenuManager menuManager = getViewSite().getActionBars()
				.getMenuManager();
		menuManager.add(actionPack);
	}

	@Override
	public void setFocus() {
		tblAlarm.setFocus();
	}

}
